package com.example.mydownloader.mymodule.sharedpreferences

import android.content.Context
import android.content.SharedPreferences
import com.example.mydownloader.config.globalConfig.GlobalParameter

/**
 * @Author: Wallace
 * @Description: SharedPreferences全局轻量级存储
 * @Date: 2021/11/21 11:29
 * @Modified: By yyyy-MM-dd
 */
object SPUtil {

    private const val TAG = "SharedPreferencesUtil"

    private val sp =
        GlobalParameter.mContext.getSharedPreferences("globalSP", Context.MODE_PRIVATE)

    private val edit: SharedPreferences.Editor = sp.edit()

    fun getStrings(vararg keys: String): HashMap<String, String> {
        val hashMap = HashMap<String, String>()
        hashMap.keys
        for (key in keys) {
            hashMap[key] = getString(key)
        }
        return hashMap
    }

    fun getString(key: String): String {
        return sp.getString(key, "") ?: ""
    }

    fun getBoolean(key: String): Boolean {
        return sp.getBoolean(key, false)
    }

    fun getLong(key: String): Long {
        return sp.getLong(key, 0L)
    }

    fun getInt(key: String): Int {
        return sp.getInt(key, 0)
    }

    fun contains(key: String): Boolean {
        return sp.contains(key)
    }

    fun setStrings(hashMap: HashMap<String,String>) {
        hashMap.forEach {
            edit.putString(it.key, it.value)
        }
        edit.apply()
    }

    fun setString(key: String, value: String) {
         edit.putString(key, value).apply()
    }

    fun setBoolean(key: String, value: Boolean) {
         edit.putBoolean(key, value).apply()
    }

    fun setLong(key: String, value: Long) {
         edit.putLong(key, value).apply()
    }

    fun setInt(key: String, value: Int) {
         edit.putInt(key, value).apply()
    }
}